#-*-coding:utf-8-*-
import os
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

class ListMode(QListWidget):
    path_Signal=Signal(str)
    def __init__(self):
        super(ListMode, self).__init__()
        self.clicked.connect(self._select_path)
        self.data={}  # {'file_dir_name':path,}

    def set_data(self,data):
        if data:
            self.clear()
            self.data=data
            for k,v in data.items():
                item=QListWidgetItem()
                item.setText(k)
                self.addItem(item)
            self.sortItems()

    def _select_path(self,index):
        # print(index)
        item=self.itemFromIndex(index)
        file_name=item.text()
        path=self.data[file_name]
        self.path_Signal.emit(path)

class PipelineTab(QDialog):

    def __init__(self):
        super(PipelineTab, self).__init__()

        self.current_selected_path=''

        self.btn_style= """QPushButton{color:black}
                                  QPushButton:hover{color:red}
                                 QPushButton{background-color:#A9A9A9}
                                  QPushButton{border:2px}
                                  QPushButton{border-radius:3px}
                                  QPushButton{padding:1px 1px}"""

        self.setWindowTitle('获取阶段资源')
        self.main_layout=QVBoxLayout()
        self.main_layout.setSpacing(20)
        self.pipeline_layout=QHBoxLayout()
        self.mm_layout=QVBoxLayout()
        self.mm_label=QLabel('mm')
        self.mm_label.setAlignment(Qt.AlignCenter)
        self.mm_list=ListMode()
        self.mm_list.path_Signal.connect(self._get_path)
        self.mm_layout.addWidget(self.mm_label)
        self.mm_layout.addWidget(self.mm_list)
        self.ani_layout=QVBoxLayout()
        self.ani_label=QLabel('ani')
        self.ani_label.setAlignment(Qt.AlignCenter)
        self.ani_list=ListMode()
        self.ani_list.path_Signal.connect(self._get_path)
        self.ani_layout.addWidget(self.ani_label)
        self.ani_layout.addWidget(self.ani_list)
        self.env_layout=QVBoxLayout()
        self.env_label=QLabel('env')
        self.env_label.setAlignment(Qt.AlignCenter)
        self.env_list=ListMode()
        self.env_list.path_Signal.connect(self._get_path)
        self.env_layout.addWidget(self.env_label)
        self.env_layout.addWidget(self.env_list)

        self.btn_layout=QHBoxLayout()
        self.open_dir_btn=QPushButton('打开目录')
        self.open_dir_btn.clicked.connect(self._open_dir)
        self.open_dir_btn.setStyleSheet(self.btn_style)
        self.load_btn=QPushButton('下载')
        self.load_btn.clicked.connect(self._load)
        self.load_btn.setStyleSheet(self.btn_style)
        self.btn_layout.addWidget(self.open_dir_btn)
        self.btn_layout.addWidget(self.load_btn)
        self.pipeline_layout.addLayout(self.mm_layout)
        self.pipeline_layout.addLayout(self.ani_layout)
        self.pipeline_layout.addLayout(self.env_layout)
        self.main_layout.addLayout(self.pipeline_layout)
        self.main_layout.addLayout(self.btn_layout)
        self.setLayout(self.main_layout)

    def init_pipeline_source(self,data):
        if data:
            for k,v in data.items():
                if k=='mm':
                    self.mm_list.set_data(v)
                if k=='ani':
                    self.ani_list.set_data(v)
                if k=='env':
                    self.env_list.set_data(v)

    def _get_path(self,path):
        print(path)
        self.current_selected_path=path

    # def _get_ani(self,ani_path):
    #     print(ani_path)
    #
    # def _get_env(self,env_path):
    #     print(env_path)


    def _open_dir(self):
        if self.current_selected_path:
            rp = self.current_selected_path.replace("/", "\\")
            try:
                os.popen(r"explorer.exe  %s" % rp)
            except Exception as _open_dir_ERR:
                print(str(_open_dir_ERR))

    def _load(self):
        pass

if __name__ == '__main__':
    app=QApplication(sys.argv)
    data={'mm':{'a.fbx':'R:/A/a.fbx','b.fbx':'R:/A/b.fbx'},'ani':{'a_v001':'R:/C/a_v001','b_v002':'R:/C/b_v002'},'env':{'e_v001':'R:/E/e_v001','e_v002':'R:/E/e_v002'}}
    p=PipelineTab()
    p.init_pipeline_source(data)
    p.show()
    sys.exit(app.exec_())
